package com.workerhelping.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.workerhelping.dto.*;
import com.workerhelping.po.Confirm;
import com.workerhelping.po.Record;
import com.workerhelping.po.Worker;
import com.workerhelping.result.Result;
import com.workerhelping.result.ResultCodeEnum;
import com.workerhelping.service.ConfirmService;
import com.workerhelping.service.RecordService;
import com.workerhelping.service.WorkerService;
import com.workerhelping.vo.WorkerPortalVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/worker")
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    @Autowired
    private ConfirmService confirmService;

    /**
     * 注册后保存职工信息
     *
     * @param worker
     * @return
     */
    @PostMapping("/saveWorker")
    public Result<String> saveWorker(@RequestBody Worker worker) {
        try {
            workerService.save(worker);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(null, ResultCodeEnum.FAIL);
        }
    }


    /**
     * 通过手机号查找用户
     *
     * @param phoneNumDTO
     * @return
     */
    @PostMapping("/getWorkerByPhoneNum")
    public Result<Worker> getWorkerByPhoneNum(@RequestBody PhoneNumDTO phoneNumDTO) {
        Worker worker = workerService.getWorkerByPhoneNum(phoneNumDTO.getPhoneNumber());
        if (worker == null)
            return Result.fail();
        return Result.ok(worker);
    }

    /**
     * 根据id查找职工
     *
     * @param workerIdDTO
     * @return
     */
    @PostMapping("/getWorkerById")
    public Result<Worker> getWorkerById(@RequestBody WorkerIdDTO workerIdDTO) {
        Worker worker = workerService.getById(workerIdDTO.getId());
        return Result.ok(worker);
    }

    /**
     * 新建档案后，补全职员空缺信息
     *
     * @param workerInfoDTO
     * @return
     */
    @PostMapping("/updateWorkerInfo")
    public Result<String> updateWorkerInfo(@RequestBody WorkerInfoDTO workerInfoDTO) {
        Worker worker = new Worker();
        BeanUtils.copyProperties(workerInfoDTO, worker);
        try {
            workerService.updateById(worker);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 职工申请提交后，将档案信息修改为”未审核“
     *
     * @param workerStatusInfoDTO
     * @return
     */
    @PostMapping("/updateWorkerStatusInfo")
    public Result<String> updateWorkerStatusInfo(@RequestBody WorkerStatusInfoDTO workerStatusInfoDTO) {
        Worker worker = new Worker();
        BeanUtils.copyProperties(workerStatusInfoDTO, worker);
        try {
            workerService.updateById(worker);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 通过workerid获取所有消息
     * @param workerIdDTO
     * @return
     */
    @PostMapping("/getConfirmByWorkerId")
    public Result<List<Confirm>> getConfirmByWorkerId(@RequestBody WorkerIdDTO workerIdDTO) {
        try {
            List<Confirm> confirms = confirmService.getConfirmByWorkerId(workerIdDTO.getId());
            return Result.ok(confirms);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

    /**
     * 逻辑删除
     * @param workerIdDTO
     */
    @PostMapping("/updateConfirmStatusByWorkerId")
    public void updateConfirmStatusByWorkerId(@RequestBody WorkerIdDTO workerIdDTO) {
        Integer workerId = workerIdDTO.getId();
        QueryWrapper<Confirm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", workerId).eq("checked", 0);
        List<Confirm> confirms = confirmService.list(queryWrapper);
        for (Confirm confirm : confirms)
            confirm.setChecked(1);
        confirmService.updateBatchById(confirms);
    }

    @PostMapping("/updateWorkerPswd")
    public Result<String> updateWorkerPswd(@RequestBody Worker worker) {
        try {
            workerService.updateById(worker);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 根据id删除帮扶信息
     *  - 逻辑删除
     *   - 0：未查看
     *   - 1：已查看
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteConfirmById(@PathVariable("id") Integer id) {
        try {
            confirmService.deleteConfirmById(id);
            return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok();
        }

    }
}
